﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CRMWeb.Pages.Test.GetResponseE_marketing
{
    public partial class GetCampaigns : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            getCampaigns();
        }

        public void getCampaigns()
        {
            String api_key = "ENTER_YOUR_API_KEY_HERE";
            String api_url = "http://api2.getresponse.com";
            JavaScriptSerializer jss = new JavaScriptSerializer();
            Hashtable _request = new Hashtable();
            try
            {
                _request["jsonrpc"] = "2.0";
                _request["id"] = 1;
                _request["method"] = "get_campaigns";
                Hashtable operator_obj = new Hashtable();
                operator_obj["EQUALS"] = "sample_marketing";
                Hashtable name_obj = new Hashtable();
                name_obj["name"] = operator_obj;
                object[] params_array = { api_key, name_obj };
                _request["params"] = params_array;
                System.Net.ServicePointManager.Expect100Continue = false;
                String response_string = getRespose(_request, api_url);
                Dictionary<string, object> jsonContent = jss.DeserializeObject(response_string) as Dictionary<string, object>;
                Dictionary<string, object> result = jsonContent["result"] as Dictionary<string, object>;
                string campaign_id = null;
                foreach (object key in result.Keys)
                {
                    campaign_id = key.ToString();
                }
                Label1.Text = campaign_id;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public string getRespose(Hashtable _request, string api_url)
        {
            JavaScriptSerializer jss = new JavaScriptSerializer();
            String response_string = null;
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(api_url);
                request.Method = "POST";
                byte[] request_bytes = Encoding.UTF8.GetBytes(jss.Serialize(_request));
                Stream request_stream = request.GetRequestStream();
                request_stream.Write(request_bytes, 0, request_bytes.Length);
                request_stream.Close();
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream response_stream = response.GetResponseStream();
                StreamReader reader = new StreamReader(response_stream);
                response_string = reader.ReadToEnd();
                reader.Close();
                response_stream.Close();
                response.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Environment.Exit(0);
            }
            return response_string;
        }
    }
}